import { request } from "../../request/index.js";
import { showModal, showToast, uploadFile, chooseImage, showLoading } from "../../util/AsyncWx.js";
let a = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    MyTopic: [],
    test: [],
    uid: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (a) {
      a = false;
      return
    } else {
      let pages = getCurrentPages();
      let currentPage = pages[pages.length - 1];
      let options = currentPage.options;
      const { id } = options;
      this.setData({
        uid: id
      })
      this.getMyTopic(id);
    }



  },
  handleImagePreview: function (e) {
    a = true
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },
  async getMyTopic(id) {

    var that = this
    const res = await request({ url: "/topic/getTopicByUid", data: { id } })
    that.setData({
      MyTopic: res
    })
  },
  async deleteTopic(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    const res = await showModal({ content: "确认删除吗？" })
    if (res.confirm) {
      const res2 = await request({ url: "/topic/deleteTopicById", data: { id } })
      var id = that.data.uid
      that.getMyTopic(id)
      await showToast({ title: "删除成功" })

    }
    // wx.showModal({
    //   title: '确认删除吗？',
    //   content: '',
    //   showCancel: true,
    //   cancelText: '取消',
    //   cancelColor: '#000000',
    //   confirmText: '确定',
    //   confirmColor: '#3CC51F',
    //   success: (result) => {
    //     if(result.confirm){
    //       wx.request({
    //         url: 'http://192.168.0.108:8080/topic/deleteTopicById',
    //         data: {id},
    //         header: {'content-type':'application/json'},
    //         method: 'GET',
    //         dataType: 'json',
    //         responseType: 'text',
    //         success: (result)=>{
    //           wx.showToast({
    //             title: '删除成功',
    //             icon: 'none',
    //             duration: 1500,
    //           });
    //           that.onShow()
    //         }
    //       });
    //     }
    //   }
    // });


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})